<template>
	<view class="pages">
		<view class="head">
			<h-line :height="statusHeight"></h-line>
			<view class="navbar">
				<view class="nav_b">
					<u-icon name="arrow-left" size="22" :bold="true" color="#202020" @click="navigateBack()"></u-icon>
				</view>
				<view class="page_title">
					<up-tabs :list="topTabList" lineWidth="30" lineHeight="5" lineColor="#00C1B9" :activeStyle="{
                color: '#00C1B9',
                fontWeight: 400,
                fontSize:'36rpx',
            }" :inactiveStyle="{
                color: '#202020',
                fontSize:'34rpx',
                fontWeight: 400,
            }" itemStyle="padding-left: 20rpx; padding-right: 20rpx; height: 100rpx;" :current="currentIndex" @click="tabsChange">
					</up-tabs>
				</view>
				<view class="nav_right">
					<view v-show="suggestionList.length>1" :style="{color:(isSort?'#00C1B9':' #202020')}" @click="bindSub">{{isSort?'完成':'排序'}}</view>
				</view>
			</view>
		</view>
		<view class="suggest_list_box" v-if="!isSort">
			<view class="suggest_list" v-for="(item, index) in suggestionList" :key="index">
				<view class="list_left">{{ item.content }}</view>
				<view class="list_right">
					<view class="sugg_box" @click="editSuggestion(item,index)">
						<image src="@/static/imgs/edit_sugg.png" mode="scaleToFill" />
						<view>编辑</view>
					</view>
					<view class="sugg_box" style="margin-left: 50rpx;" @click="dleSuggestion(item,index)">
						<image src="@/static/imgs/del_sugg.png" mode="scaleToFill" />
						<view>删除</view>
					</view>
				</view>
			</view>
			<h-line :height="bottomValue+112"></h-line>
		</view>
		<view v-if="isSort" class="sort_list">
			<basic-drag :value="suggestionList" itemKey="content" :column="1" @input="newList" itemHeight="120px">
				<template #item="{element}">
					<view class="drag_list">
						<view class="list_left">{{ element.content }}</view>
						<view class="list_right">
							<image src="@/static/imgs/sort_icon.png" mode="scaleToFill" />
							<text>拖动排序</text>
						</view>
					</view>
				</template>
			</basic-drag>
		</view>
		<view class="list_none" v-if="suggestionList.length==0">
			<view>
				<image class="list_none_img" src="@/static/images/nones.png"></image>
				<view class="list_none_text">暂无内容</view>
			</view>
		</view>
		<view class="add_btn_box" v-if="!isSort">
			<view v-if="!isRecover" class="add_text" @click="navigateTo(`/common/suggest/add?title=添加常用建议`)">添加常用建议</view>
			<view v-else class="add_text" @click="navigateTo(`/common/suggest/add?title=添加常用回复`)">添加常用回复</view>
			<h-line :height="bottomValue"></h-line>
		</view>
	</view>
	<!-- 删除提示 -->
	<h-modal :show="delBindShow" titlea="是否确认删除" leftBtn="删除" rightBtn="取消" leftColor="#DE272A" rightColor="#5C5C5C" @clear="saveEditFn" @bind="delBindShow = false "></h-modal>
</template>

<script setup lang="ts">
	import { ref,watch } from 'vue'
	import { navigateBack, navigateTo, formatDate, showToast } from '@/utils'
	import { onLoad, onPageScroll, onShow, onReady } from '@dcloudio/uni-app'
	import BasicDrag from '@/components/basic-drag/index.vue';
	import { commonToolsMessageConfigList,commonToolsSortMessageConfig,commonToolsDeleteMessageConfig } from '@/api/suggest'
	const statusHeight = ref(0)
	const scrollBgc = ref(false)
	//是否排序
	const isSort = ref(false)
	//是否回复
	const isRecover = ref(false)
	//删除提示
	const delBindShow = ref(false)
	//topTab
	const topTabList = ref([{
			name: '常用建议'
		},
		{
			name: '常用回复'
		},
	])
	const suggestionList = ref([])
	const currentIndex=ref(0)
	//底部安全距离
	const bottomValue = ref(0)
	onReady(() => {
		uni.getSystemInfo({
			success: (e) => {
				statusHeight.value = e?.statusBarHeight * 2
				bottomValue.value = e?.safeAreaInsets?.bottom*2
			},
		})
	})
	onLoad((o) => {
		if(o?.type){
      currentIndex.value=Number(o?.type - 1)
      if(currentIndex.value == 0){
        isRecover.value == false
      }else{
        isRecover.value = true
      }
			getList()
		}else{

		}
		addListener()
	})
	function addListener() {
		uni.$on('addMessage', (row) => {
			suggestionList.value.unshift({
				id:row.id,
				content:row.content
			})
		})
		uni.$on('editMessage', (row) => {
			console.log(row)
			const index = suggestionList.value.findIndex((item) => item.id == row.id)
			if(index != -1) {
				suggestionList.value[index].content = row.content
			}
		})
	}
	async function getList(){
		const res=await commonToolsMessageConfigList({type:currentIndex.value+1})
		suggestionList.value=res.data
	}
	async function newList(e){
		let arr=[]
		for(var i in e){
			arr.push(e[i].id)
		}
		await commonToolsSortMessageConfig({sort:arr})
	}
	function tabsChange(e) {
		console.log(e)
		if(e.index == 0) {
			isRecover.value = false
		} else {
			isRecover.value = true
		}
		currentIndex.value=e.index
		getList()
	}
	//编辑
	function editSuggestion(item,index) {
		if(!isRecover.value) {
			navigateTo(`/common/suggest/add?title=编辑常用建议&id=${item.id}&content=${item.content}`)
		} else {
			navigateTo(`/common/suggest/add?title=编辑常用回复&id=${item.id}&content=${item.content}`)
		}
	}
	const delDetail=ref({})
	const delIndex=ref(0)
	//删除
	function dleSuggestion(item,index) {
		delBindShow.value = true
		delIndex.value=index
		delDetail.value=item
	}
	function bindSub(){
		if(isSort.value){
			getList()
		}
		isSort.value=!isSort.value
	}

	async function saveEditFn() {
		const res=await commonToolsDeleteMessageConfig({
			id:delDetail.value.id
		})
		showToast('删除成功')
		suggestionList.value.splice(delIndex.value,1)
		delBindShow.value = false
	}
</script>

<style lang="scss" scoped>
.list_none{
	display: flex;
		flex-direction: row;
		/* 子元素横向排列 */
		justify-content: center;
		/* 相对父元素水平居中 */
		align-items: center;
		.list_none_img {
				width: 250rpx;
				height: 200rpx;
				display: block;
				margin-top: 30vh;
			}
			.list_none_text {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 26rpx;
				color: #aaaaaa;
				line-height: 37rpx;
				text-align: center;
				margin-top: 20rpx;
			}
}
.head{
	position: sticky;
	top: 0;
	z-index: 1000;
	background: #FFF;
	left: 0;
	.navbar {
			padding: 0 24rpx;
			width: 100vw;
			height: 100rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			box-sizing: border-box;
			font-family: PingFang SC, PingFang SC;
			.page_title {
				width: 380rpx;
				display: flex;
			}
			.nav_b {
				width: 100rpx;
				height: 32rpx;
				padding: 30rpx 0;
			}
			.nav_right {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 100rpx;
				font-weight: 400;
				font-size: 30rpx;
				color: #202020;
				.right_item {
					display: flex;
					flex-direction: column;
					align-items: center;
					margin-left: 56rpx;
					image {
						width: 35rpx;
						height: 35rpx;
						margin-bottom: 4rpx;
					}
				}
			}
		}
}
	.pages {
		width: 750rpx;
		min-height: 100vh;
		background: #F5F6F8;
		display: flex;
		flex-direction: column;
		align-items: center;
		font-family: PingFangSC, PingFang SC;
		position: relative;
		.suggest_list_box{
			background: #F5F6F8;
		}
		.suggest_list {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			width: 750rpx;
			background: #FFFFFF;
			margin-bottom: 20rpx;
			padding: 24rpx;
			.list_left {
				align-self: flex-start;
				font-weight: 400;
				font-size: 26rpx;
				color: #202020;
				margin-bottom: 36rpx;
			}
			.list_right {
				display: flex;
				align-items: center;
				align-self: flex-end;
				image {
					width: 26rpx;
					height: 26rpx;
					margin-right: 10rpx;
				}
				.sugg_box {
					display: flex;
					align-items: center;
					font-weight: 400;
					font-size: 26rpx;
					color: #5C5C5C;
				}
			}
		}
		.add_btn_box {
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 1000;
			width: 750rpx;
			background: #FFFFFF;
			.add_text {
				width: 702rpx;
				height: 88rpx;
				background: #00C1B9;
				border-radius: 48rpx;
				margin-left: 24rpx;
				margin-top: 10rpx;
				text-align: center;
				line-height: 88rpx;
				margin: 12rpx auto;
				font-weight: 500;
				font-size: 32rpx;
				color: #FFFFFF;
			}
		}
		.sort_list {
			width: 750rpx;
			background: #F5F6F8;
			.drag_list {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				width: 750rpx;
				background: #FFFFFF;
				padding: 24rpx;
				.list_left {
					align-self: flex-start;
					font-weight: 400;
					font-size: 26rpx;
					color: #202020;
					height: 74rpx;
					width: 100%;
					margin-bottom: 30rpx;
					overflow: hidden;
					display:-webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
				}
				.list_right {
					display: flex;
					align-items: center;
					align-self: flex-end;
					image {
						width: 26rpx;
						height: 26rpx;
						margin-right: 10rpx;
					}
				}
			}
		}
	}
</style>
